#coding=utf-8
#author：zhujingyuan

import cx_Oracle


class OracelExcute:

    def __init__(self,user,password,ip,port,servicename):
        self.ip=ip
        self.user=user
        self.password=password
        self.port=port
        self.servicename=servicename


    def __getConnect(self):
        if not self.servicename:
            raise NameError("没有设置数据库信息")
        # self.dsn=cx_Oracle.makedsn(host=self.host,port=self.port,servicename=self.servicename)

        self.conn=cx_Oracle.connect(self.user+"/"+self.password+"@"+self.ip+":"+self.port+"/"+self.servicename)

        self.cursor=cx_Oracle.Cursor(self.conn)

        if not self.cursor:
            raise NameError("连接数据库失败")
        else:
            return self.cursor
    #查询一条记录
    def execQueryOne(self,sql):
        cur=self.__getConnect()
        cur.execute(sql)
        cur2=cur.fetchone()
        row=cur2[0]
        if row:
            return row
        self.cursor.close()
        self.conn.close()

    #查询多条记录
    def execQueryMany(self,sql):
        cur=self.__getConnect()
        cur.execute(sql)
        cur2=cur.fetchall()
        row=cur2[0:9]
        if row:
            return row
        self.cursor.close()
        self.conn.close()

    #非查询操作
    def execNonQuery(self,sql):
        cur=self.__getConnect()
        cur.execute(sql)
        self.conn.commit()
        self.conn.close()



#调试
# oracle=OracelExcute(user='oms',password='oms2008',ip='192.168.18.182',port='1521',servicename='qasthird')
# result=oracle.execQueryOne("SELECT DEPARTMENTCODESTORE FROM OM_ORDERCHILD where ORDERNO  in (select orderno from om_orderchilddetail where orderchilddetailid='242845')")
# print(result)
# print(type(result))























